package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ItemTeleportTable {
  private static final Log _log = LogFactory.getLog(ItemTeleportTable.class);
  
  private static final Map<Integer, TeleportList> _teleportList = new HashMap<>();
  
  public static boolean START = false;
  
  private static ItemTeleportTable _instance;
  
  public static ItemTeleportTable get() {
    if (_instance == null)
      _instance = new ItemTeleportTable(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `etcitem_teleport`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        int item_id = rs.getInt("item_id");
        int locx = rs.getInt("locx");
        int locy = rs.getInt("locy");
        short mapid = rs.getShort("mapid");
        int time = rs.getInt("time");
        int week = rs.getInt("限制星期");
        int starttime = rs.getInt("几点开始");
        int unitytime = rs.getInt("几点结束");
        TeleportList list = new TeleportList();
        list._locx = locx;
        list._locy = locy;
        list._mapid = mapid;
        list._time = time;
        list._week = week;
        list._starttime = starttime;
        list._unitytime = unitytime;
        _teleportList.put(Integer.valueOf(item_id), list);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    _log
      .info("读取=>传送卷轴传送点定义数量: " + _teleportList.size() + "(" + timer.get() + "ms)");
    if (_teleportList.size() <= 0) {
      _teleportList.clear();
    } else {
      START = true;
    } 
  }
  
  public TeleportList getLoc(int item_id) {
    TeleportList teleport = null;
    if (_teleportList.containsKey(Integer.valueOf(item_id)))
      teleport = _teleportList.get(Integer.valueOf(item_id)); 
    return teleport;
  }
  
  public class TeleportList {
    private int _locx;
    
    private int _locy;
    
    private short _mapid;
    
    private int _time;
    
    private int _week;
    
    private int _starttime;
    
    private int _unitytime;
    
    public int getLocX() {
      return this._locx;
    }
    
    public int getLocY() {
      return this._locy;
    }
    
    public short getMapId() {
      return this._mapid;
    }
    
    public int getTime() {
      return this._time;
    }
    
    public int getWeek() {
      return this._week;
    }
    
    public int getStarttime() {
      return this._starttime;
    }
    
    public int getUnitytime() {
      return this._unitytime;
    }
  }
}
